package com.huafeng.security;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.Objects;

/**
 * @author 花风
 * @date 2023/8/18 17:22
 * @Description 自定义加密器
 */
@Component
public class CustomPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence rawPassword) {
        return DigestUtils.md5DigestAsHex(String.valueOf(rawPassword).getBytes());
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        String newPassword = DigestUtils.md5DigestAsHex(String.valueOf(rawPassword).getBytes());
        return Objects.equals(newPassword,encodedPassword);
    }
}
